<template>
  <div class="transaction-surveillance container pb">
    <p class="bread">
      <a href="#/personalcenter">个人中心</a> >
      <a href="#/personalcenter/transac" class="font-s-18">订单管理</a> >
      <a href="javascript:void(0);" class="font-s-16">交易监控</a>
    </p>
    <div class="bg-c-fff box-border">
      <h3 class="tm-title">交易监控服务</h3>
      <step :stepData="stepData" :stepNum="stepNum" @stepRouter="stepRouter"></step>
    </div>
    <div class="surveillance-title-box">
      <div class="surveillance-title">交易监控流程图</div>
      <div class="btn-box" v-if="status === 5">
        <button type="button" class="white-button" @click="msgShow = true">发送消息</button>
        <button type="button" class="white-button" @click="errorShow = true">客户纠错</button>
      </div>
    </div>
    <div class="progress-box">
      <company-name-container :companyNames="companyNames"></company-name-container>
      <div class="line" :class="{center: companyNames.length===2}" :style="{height: (events.length+0.5)*90+60+'px'}" v-if="events.length">
        <div class="arrow"></div>
      </div>
      <event-item v-for="(item,index) in events" :key="index" :state="item.state" :totalSides="companyNames.length" :side="item.side" :content="item.content" :files="item.files" :currentSide="getSide(item.side)" :allState="3" :eventId="item.id" tradeType="trade" @fileUploaded="onFileUploaded" :serviceId="id"></event-item>
      <div class="submit-box" :class="{'text-center': companyNames.length===2}" v-show="events.length && status === 5">
        <button type="button " class="blue-button xl-btn-css " @click="confirmShow=true ">交易完成</button>
      </div>
    </div>
    <vodal :show="msgShow " animation="zoom " :width="380 " :height="283 " @hide="msgShow=false ">
      <div class="vodal-main vodal-xxbox ">
        <h4>发送消息</h4>
        <ul class="con ">
          <li>消息内容：
            <textarea class="vt " placeholder="请输入消息内容，我们将发送给信宝相关工作人员处理 " v-model="msgContent "></textarea>
          </li>
        </ul>
        <div class="tc auto vodal-btn-box ">
          <button class="blue-button " type="button " @click="onMsgSubmit ">确认</button>
          <button class="white-button " @click="msgShow=false ">取消</button>
        </div>
      </div>
    </vodal>
    <vodal :show="errorShow " animation="zoom " :width="380 " :height="290 " @hide="errorShow=false ">
      <div class="vodal-main vodal-errorbox ">
        <h4>客户纠错申请</h4>
        <ul class="con ">
          <li>
            纠错内容：
            <textarea class="vt " placeholder="请输入纠错原因 " v-model="errorContent"></textarea>
          </li>
          <li>上传交易合同：
            <input type="button" value="选择文件" class="blue-button small-btn-css" id="uploaderBtn">
          </li>
        </ul>
        <div class="tc auto vodal-btn-box ">
          <button typr="button " class="blue-button " @click="onErrorSubmit">确认上传</button>
        </div>
      </div>
    </vodal>
    <vodal :show="confirmShow " animation="zoom " :width="380 " :height="125 " @hide="confirmShow=false ">
      <div class="vodal-main ">
        <h4>您确认订单已完成吗？</h4>
        <div class="tc auto vodal-btn-box mt ">
          <button typr="button " class="blue-button " @click="onConfirmSubmit">确认</button>
          <button typr="button " class="blue-button " @click="confirmShow=false ">取消</button>
        </div>
      </div>
    </vodal>
  </div>
</template>
<script type="text/javascript ">
import Vodal from 'Vodal'
import {
  mapState
} from 'vuex'
import Step from '../components/Step'
import CompanyNameContainer from '../components/CompanyNameContainer'
import EventItem from '../components/EventItem'
import FileUploadManager from '../components/FileUploaderManager'
export default {
  components: {
    Vodal,
    Step,
    CompanyNameContainer,
    EventItem
  },
  data() {
    return {
      id: '',
      stepNum: 4,
      stepData: [{
        name: '提交服务申请'
      }, {
        name: '确认资质报告'
      }, {
        name: '合同审查'
      }, {
        name: '绘制流程图'
      }, {
        name: '交易监控'
      }, {
        name: '服务评价'
      }],
      errorShow: false,
      confirmShow: false,
      msgShow: false,
      msgContent: '',
      errorContent: '',
      errorfile: '',
      status: 0,
      companyNames: [],
      currentSide: -1,
      events: [],
      monitorType: 0
    }
  },
  computed: {
    ...mapState({
      userId: (s) => {
        return s.userInfo.id
      }
    })
  },
  mounted() {
    this.id = this.$route.query.id
    this.initUploader()
    this.getData()
  },
  methods: {
    getSide(side) {
      if (this.monitorType === 1) {
        return side
      } else {
        return this.currentSide
      }
    },
    onFileUploaded() {
      this.getData()
    },
    stepRouter(i, name) {
      if (i === 0 && this.status >= 1) {
        this.$router.push('/submit-apply?id=' + this.$route.query.id)
      }
      if (i === 1 && this.status >= 2) {
        this.$router.push('/confirm-aptitude?id=' + this.$route.query.id)
      }
      if (i === 2 && this.status >= 3) {
        this.$router.push('/contract-review?id=' + this.$route.query.id)
      }
      if (i === 3 && this.status >= 4) {
        this.$router.push('/transac-service-wait?id=' + this.$route.query.id)
      }
      if (i === 4 && this.status >= 5) {
        this.$router.push('/transactionsurveillance?id=' + this.$route.query.id)
      }
      if (i === 5 && this.status >= 6) {
        this.$router.push('/service-evalua?id=' + this.$route.query.id)
      }
    },
    onMsgSubmit() {
      if (!this.msgContent) {
        this.$toasted.show('请输入消息内容')
        return
      }
      this.$http.post('/message/send/message/byorder', {
        orderid: this.$route.query.id,
        orderType: 2,
        msgKind: 1,
        sendType: 1,
        msgContent: this.msgContent
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.$toasted.show('发送成功')
          this.msgShow = false
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    onErrorSubmit() {
      if (!this.errorContent) {
        this.$toasted.show('请输入纠错原因')
        return
      }
      if (!this.errorfile) {
        this.$toasted.show('请上传交易合同')
        return
      }
      this.$http.post('/tradeError/send/trade/error', {
        orderid: this.$route.query.id,
        recivedType: 1,
        errorContent: this.errorContent,
        errorfile: this.errorfile
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.$toasted.show('发送成功')
          this.errorShow = false
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    getData() {
      this.$http.post('/transactionMonitorSer/business/read/detail', {
        id: this.$route.query.id
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.monitorType = data.data.data.monitorType
          this.status = data.data.data.status
          this.events = []
          data.data.data.eventList.forEach(item => {
            let event = {}
            event.content = item.content
            event.side = item.memberOrder
            event.state = item.status
            event.files = item.fileList || []
            event.index = item.order
            event.id = item.id
            this.events.push(event)
          })
          this.companyNames = []
          data.data.data.memberList.forEach((member, index) => {
            this.companyNames.push({
              name: member.memberName,
              id: member.memberId
            })
            if (member.memberId === this.userId && this.status === 5) {
              this.currentSide = index
            }
          })
        } else {
          this.$toasted.show(data.data.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    },
    initUploader() {
      this.uploader = new FileUploadManager('error-file', 'uploaderBtn', (url) => {
        this.errorfile = url
        console.log(this.errorfile)
        this.$toasted.show('上传成功')
      }, (msg) => {
        this.$toasted.show(msg)
      }, true)
    },
    onConfirmSubmit() {
      this.$http.post('/transactionMonitorSer/business/finish', {
        id: this.$route.query.id
      }).then(data => {
        if (data.data.httpCode === 200) {
          this.$toasted.show('提交成功!')
          if (data.data.data.monitorType === 1) {
            this.$router.push('/service-evalua?id=' + this.$route.query.id)
          } else {
            this.$router.push('/personalcenter/transac')                                                       
          }
        } else {
          this.$toasted.show(data.data.msg)
          this.$router.push('/personalcenter/transac')
        }
      }).catch(err => {
        console.log(err)
        this.$toasted.show('网络出错')
      })
    }
  }
}

</script>
<style type="text/css " scoped>
.tm-title {
  line-height: 40px;
  padding-left: 35px;
  font-size: 14px;
  color: #282828;
  border-bottom: 1px solid #ddd;
}

.pb {
  padding-bottom: 70px;
}

.surveillance-title-box {
  position: relative;
  width: 100%;
  height: 40px;
  line-height: 40px;
  margin-top: 20px;
  border-bottom: 1px solid #ddd;
  background-color: #fff;
  text-align: center;
}

.surveillance-title {
  font-size: 14px;
  color: #282828;
}

.surveillance-title-box .btn-box {
  position: absolute;
  top: 0;
  right: 75px;
  font-size: 14px;
}

.surveillance-title-box .white-button {
  width: 100px;
  height: 30px;
  line-height: 30px;
  margin-left: 8px;
}

.progress-box {
  position: relative;
  width: 100%;
  background-color: #fff;
  overflow-x: auto;
  padding-bottom: 50px;
}

.line {
  position: absolute;
  width: 2px;
  top: 0;
  left: 65px;
  margin-top: 10px;
  background-color: #cfcfcf;
}

.line.center {
  left: 50%;
}

.line .arrow {
  position: absolute;
  bottom: -10px;
  left: -7px;
  width: 0;
  height: 0;
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-top: 12px solid #cfcfcf;
}

.submit-box {
  width: 100%;
  padding: 40px;
  margin-top: 40px;
}

.text-center {
  text-align: center;
}

.vodal-xxbox textarea {
  width: 256px;
  height: 120px;
}

.vodal-errorbox textarea {
  width: 256px;
  height: 80px;
}

.vodal-errorbox input {
  margin-left: 17px;
}

.content {
  margin-top: 30px;
}

.content h4 {
  padding-left: 30px;
  font-size: 14px;
  line-height: 30px;
  border-bottom: 1px solid #ddd;
}

.note-box {
  padding: 10px;
  text-indent: 20px;
  line-height: 30px;
}

.mt {
  margin-top: 27px;
}

</style>
